package com.sfh.story;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import cn.bmob.v3.listener.SaveListener;

import com.sfh.story.entrty.Story;
import com.sfh.story.entrty.StoryDetil;

public class StotyList extends FragmentActivity
{
    
    private ListView mListView;
    private List<Story> mList = new ArrayList<Story>();
    private StoryAdapter mAdapter;
    public static final String KEY = "url";
    public static final String TYPE = "type";
    private ProgressBar progressBar;
    private int type = 1;
    
    private int posistion = 0;
    
    private View view;
    @Override
    protected void onCreate(Bundle arg0)
    {
        super.onCreate(arg0);
        setContentView(R.layout.story_list);
        mListView = (ListView)findViewById(R.id.ls);
        progressBar = (ProgressBar)findViewById(R.id.progressBar1);
        init();
        view =findViewById(R.id.bt);
        view .setOnClickListener(new View.OnClickListener()
        {
            
            @Override
            public void onClick(View v)
            {
                view.setEnabled(false);
                add();
            }
        });
    }
    
    public void toast(String text)
    {
        Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
    }
    
    private void init()
    {
        mAdapter = new StoryAdapter();
        mListView.setAdapter(mAdapter);
        String url = getIntent().getStringExtra(KEY);
        type = getIntent().getIntExtra(TYPE, 1);
        progressBar.setVisibility(View.VISIBLE);
        new LoadHtml().start(url, new LoadHtml.IStoryList()
        {
            @Override
            public void load(List<Story> list)
            {
                mList.addAll(list);
                mAdapter.notifyDataSetChanged();
                progressBar.setVisibility(View.GONE);
            }
        });
    }
    
    StoryDetil detil;
  
    private void add()
    {
        if (posistion == mList.size())
        {
            view.setEnabled(true);
            toast("全部成功");
            progressBar.setVisibility(View.GONE);
            return;
        }
        Story story = mList.get(posistion);
        progressBar.setVisibility(View.VISIBLE);
        new GetStoryContent().start(story, new GetStoryContent.IStory()
        {
            @Override
            public void load(Story story)
            {
                 add2(story);
            }
        });
    }
    
    void add2(final  Story mStory)
    {
        detil = new StoryDetil();
//        detil.setIcon(mStory.imgs.get(0));
        detil.setDesc(mStory.content.get(0));
        detil.setName(mStory.name);
        detil.setReadSize(100);
        detil.setType(type);
        detil.save(StotyList.this, new SaveListener()
        {
            
            @Override
            public void onSuccess()
            {
                Mananger.addStoryContent(StotyList.this, new SaveListener()
                {
                    
                    @Override
                    public void onSuccess()
                    {
                        toast("成功:"+detil.getName());
                        posistion ++;
                        add();
                    }
                    
                    @Override
                    public void onFailure(int arg0, String arg1)
                    {
                        view.setEnabled(true);
                        progressBar.setVisibility(View.GONE);
                        toast("失败");
                    }
                }, mStory, detil.getObjectId());
                
            }
            
            @Override
            public void onFailure(int arg0, String arg1)
            {
                view.setEnabled(true);
                progressBar.setVisibility(View.GONE);
                toast("失败");
            }
        });
    }
    
    
    class StoryAdapter extends BaseAdapter
    {
        
        @Override
        public int getCount()
        {
            return mList.size();
        }
        
        @Override
        public Object getItem(int position)
        {
            return mList.get(position);
        }
        
        @Override
        public long getItemId(int position)
        {
            return position;
        }
        
        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            HandlerView mHandlerView = null;
            if (convertView == null)
            {
                convertView = getLayoutInflater().inflate(R.layout.item_view, null);
                mHandlerView = new HandlerView();
                mHandlerView.tv = (TextView)convertView.findViewById(R.id.tv);
                convertView.setTag(mHandlerView);
            }
            else
            {
                mHandlerView = (HandlerView)convertView.getTag();
            }
            mHandlerView.tv.setText(mList.get(position).name);
            return convertView;
        }
        
        class HandlerView
        {
            TextView tv;
        }
        
    }
}
